package com.ian.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Witt
 * @version 1.0.0
 * @date 2022/5/5
 */
//@Controller
@RestController
public class IndexController {

    @Autowired
    RestTemplate restTemplate;

    /*@GetMapping("/index.html")
    public String index() {
        return "index";
    }*/

    /*@GetMapping("/")
    public String index() {
        return "forward:/index.html";
    }*/

    @GetMapping("/authorization_code")
    public String authorization_code(String code) throws IOException {
        Map<String, String> map = new HashMap<>();
        // github
//        map.put("client_id", "b640eab18c1fad969a66");
//        map.put("client_secret", "8731039d803c29133829b8252a6669b7e6608667");
        // gitee
        map.put("client_id", "1cea1866b018f1fe14c9c10383186a67285023245aba1583f99b14b2a18fdd61");
        map.put("client_secret", "ef32df4d3c273e6e468629fe6c8270834f5b42f67bc8e81982752704ca8f9349");
        map.put("state", "witt");
        map.put("code", code);
        map.put("redirect_uri", "http://localhost:7900/authorization_code");
        // get access_token
        // github
//        Map<String, String> resp = restTemplate.postForObject("https://github.com/login/oauth/access_token", map, Map.class);
        // gitee
        Map<String, String> resp = restTemplate.postForObject("https://gitee.com/oauth/token?grant_type=authorization_code", map, Map.class);
        System.out.println("resp: " + resp);

        // github 将 access_token 添加到 请求头中，gitee 既可以将 access_token 添加到 请求头中，也可以拼接在url中
        HttpHeaders httpheaders = new HttpHeaders();
        httpheaders.add("Authorization", "token " + resp.get("access_token"));
        HttpEntity<?> httpEntity = new HttpEntity<>(httpheaders);

        // 获取用户信息
        // github
//        ResponseEntity<Map> exchange = restTemplate.exchange("https://api.github.com/user", HttpMethod.GET, httpEntity, Map.class);
        // gitee
//        ResponseEntity<Map> exchange = restTemplate.exchange("https://gitee.com/api/v5/user?access_token=" + resp.get("access_token"), HttpMethod.GET, httpEntity, Map.class); 这样也行
//        ResponseEntity<Map> exchange = restTemplate.exchange("https://gitee.com/api/v5/user", HttpMethod.GET, httpEntity, Map.class);
        ResponseEntity<ArrayList> exchange = restTemplate.exchange("https://gitee.com/api/v5/users/mediocrep/repos", HttpMethod.GET, httpEntity, ArrayList.class);

        // 获取项目信息的 url 是啥 ???
//        ResponseEntity<Map> exchange = restTemplate.exchange("https://gitee.com/api/v5/projects", HttpMethod.GET, httpEntity, Map.class);
        String respBodyStr = new ObjectMapper().writeValueAsString(exchange.getBody());
        System.out.println("exchange.getBody() = " + respBodyStr);
        /*
        gitee output: exchange.getBody() =
        {"id":2353624,"login":"mediocrep","name":"Witt","avatar_url":"https://gitee.com/assets/no_portrait.png",
                "url":"https://gitee.com/api/v5/users/mediocrep","html_url":"https://gitee.com/mediocrep","remark":"",
                "followers_url":"https://gitee.com/api/v5/users/mediocrep/followers",
                "following_url":"https://gitee.com/api/v5/users/mediocrep/following_url{/other_user}",
                "gists_url":"https://gitee.com/api/v5/users/mediocrep/gists{/gist_id}",
                "starred_url":"https://gitee.com/api/v5/users/mediocrep/starred{/owner}{/repo}",
                "subscriptions_url":"https://gitee.com/api/v5/users/mediocrep/subscriptions",
                "organizations_url":"https://gitee.com/api/v5/users/mediocrep/orgs",
                "repos_url":"https://gitee.com/api/v5/users/mediocrep/repos",  获取所有的仓库列表
                "events_url":"https://gitee.com/api/v5/users/mediocrep/events{/privacy}",
                "received_events_url":"https://gitee.com/api/v5/users/mediocrep/received_events",
                "type":"User","blog":null,"weibo":null,"bio":"","public_repos":43,"public_gists":0,"followers":0,
                "following":1,"stared":4,"watched":69,"created_at":"2018-12-12T11:22:37+08:00","updated_at":"2022-05-15T23:46:22+08:00","email":null}
         */
//        return "forward:/index.html";
        return respBodyStr;
    }
}
